package com.hooya.fa.eu.biz.dao.manager.special;

import com.hooya.fa.eu.api.bo.stk.BuyRecordBO;
import com.hooya.fa.eu.api.enums.CkEnum;
import com.hooya.fa.eu.api.enums.CountryEnum;
import com.hooya.fa.eu.biz.dao.entity.*;
import com.hooya.fa.eu.biz.dao.manager.GeneratorEntityManager;
import com.hooya.fa.eu.biz.service.TempRelationService;
import com.hooya.integrated.common.core.util.RedisUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

import static com.hooya.fa.eu.api.constant.DictConstant.DICT_PREFIX;

/**
 * @Description 1
 * @Author : Zongz
 * @Copyright Copyright (c) 豪雅集团
 * @Create 2024/2/27
 * @Version 1.0.0
 */
@Slf4j
@Component
@AllArgsConstructor
public class ZyDataManager {

    private final GeneratorEntityManager entityManager;

    private final ZyHisDataManager zyHisDataManager;

    private final TempRelationService tempRelationService;

    /**
     * 去除转入转出为restock的转运数据
     *
     * @param zyList            转运数据
     * @param placeMatchDictMap place匹配字典
     */
    public void removeRestockData(List<BuyRecordBO> zyList, Map<String, PlaceMatchDict> placeMatchDictMap) {
        Iterator<BuyRecordBO> iterator = zyList.iterator();
        while (iterator.hasNext()) {
            BuyRecordBO bo = iterator.next();
            PlaceMatchDict zl = placeMatchDictMap.get(bo.getZlck());
            PlaceMatchDict zc = placeMatchDictMap.get(bo.getZcck());
            if (Objects.nonNull(zl) && Objects.nonNull(zc)) {
                if (zc.getType().equals("restock") || zl.getType().equals("restock")) {
                    iterator.remove();
                }
            }
        }
    }

    /**
     * 处理进仓数据，转变为买卖数据（前置处理）
     *
     * @param fbaData          是否为fba数据
     * @param zyData           转运数据
     * @param inventoryList    仓库数据
     * @param tradeRecordList  买卖记录
     * @param tempZyjlList     转运记录
     * @param metaWarehouseMap 仓库国家字典
     * @param zyjlChangeList   非fba转运平仓数据
     * @param deleteZyRecord   fba转运删除数据（fba用）
     * @param fbaHisJcZy       fba历史进仓转运数据（fba用）
     * @param pkpyList         盘亏盘盈集合（fba用）
     */
	public void dealWithZyData(Boolean fbaData, List<BuyRecordBO> zyData, List<TempKcjlDaily> inventoryList,
							   List<TradeRecord> tradeRecordList, List<TempZyjl> tempZyjlList,
							   Map<String, String> metaWarehouseMap, List<TempZyjlChange> zyjlChangeList,
							   List<BuyRecordBO> deleteZyRecord, List<BuyRecordBO> fbaHisJcZy, List<TempPkpy> pkpyList) {
		Integer cgsl;
		if (!CollectionUtils.isEmpty(zyData)) {
			Map<String, String> fbaCkMap = RedisUtils.hmget(DICT_PREFIX + CkEnum.FBA_CK_MAP.getName());
			Map<String, List<String>> fbaCkFdsMap =
					new TreeMap<>(RedisUtils.hmget(DICT_PREFIX + CkEnum.FBA_CK_REVERSAL_MAP.getName()));
			Map<String, Map<String, List<String>>> fbaCkCostwayMap =
					new TreeMap<>(RedisUtils.hmget(DICT_PREFIX + CkEnum.FBA_CK_COSTWAY_MAP.getName()));
			for (BuyRecordBO zy : zyData) {
				int zcckTotalNum = 0;
				cgsl = zy.getCgsl();
				String zcOrigin = metaWarehouseMap.get(zy.getZcck());
				String zlOrigin = metaWarehouseMap.get(zy.getZlck());
				long num = Optional.of(inventoryList.stream().filter(l ->
						l.getCpbh().equals(zy.getCpbh()) && l.getPlace().equals(zy.getZcck()) &&
								l.getBelongs().equals("FDS-" + zcOrigin)).count()).orElse(0L);
				if (num > 0 && cgsl > 0) {
					int sum = inventoryList.stream().filter(l ->
							l.getCpbh().equals(zy.getCpbh()) && l.getPlace().equals(zy.getZcck()) &&
									l.getBelongs().equals("FDS-" + zcOrigin)).mapToInt(TempKcjlDaily::getSl).sum();
					zcckTotalNum += sum;
					cgsl = dealWithZyData(zy, inventoryList, tradeRecordList, tempZyjlList,
							sum, cgsl, true, zcOrigin, zlOrigin, "");
				}
				//双重校验，优化用
				for (String origin : CountryEnum.getCountryList()) {
					if (cgsl <= 0) {
						break;
					}
					String realBelongs = "COSTWAY-" + origin;
					num = Optional.of(inventoryList.stream().filter(l ->
							l.getCpbh().equals(zy.getCpbh()) && l.getPlace().equals(zy.getZcck()) &&
									l.getBelongs().equals(realBelongs)).count()).orElse(0L);
					if (num > 0) {
						int sum = inventoryList.stream().filter(l ->
								l.getCpbh().equals(zy.getCpbh()) && l.getPlace().equals(zy.getZcck()) &&
										l.getBelongs().equals(realBelongs)).mapToInt(TempKcjlDaily::getSl).sum();
						zcckTotalNum += sum;
						cgsl = dealWithZyData(zy, inventoryList, tradeRecordList, tempZyjlList,
								sum, cgsl, false, zcOrigin, zlOrigin, origin);
					}
				}
				//cgsl还是>0
				if (cgsl > 0) {
					if (!fbaData) {
						//不是fba的情况，改数据操作，将转出仓库的数据抹平。
						zy.setCgsl(zcckTotalNum);
						TempZyjlChange change = TempZyjlChange.builder().sheetNumber(zy.getSheetNumber())
								.hthm(zy.getHthm()).zcck(zy.getZcck()).zlck(zy.getZlck()).htrq(zy.getHtrq())
								.cpbh(zy.getCpbh()).cgsl(zy.getCgsl()).createTime(new Date()).build();
						zyjlChangeList.add(change);
					} else {
						//处理当前其他仓库
						cgsl = dealFbaOtherCK(cgsl, zy, inventoryList, tradeRecordList, tempZyjlList,
								metaWarehouseMap, fbaCkMap, fbaCkFdsMap, fbaCkCostwayMap);
					}
				}
				if (cgsl > 0 && fbaData) {
					zy.setCgsl(cgsl);
					zyHisDataManager.dealWithHisZyData(zy, fbaHisJcZy, inventoryList, tradeRecordList,
							tempZyjlList, metaWarehouseMap, deleteZyRecord, pkpyList, fbaCkCostwayMap,
							fbaCkMap, fbaCkFdsMap);
				}
			}
		}
	}

	/**
     * 处理进仓数据，转变为买卖数据（数据处理）
     *
     * @param zy              转运数据
     * @param inventoryList   仓库数据
     * @param tradeRecordList 买卖记录
     * @param tempZyjlList    转运记录
     * @param kcsl            仓库数量（一定是大于0）
     * @param cgsl            采购数量（一定是大于0）
     * @param flag            true：fds；false：costway
     * @param origin          如果有则值为[PL, FR, DE, IT]
     * @return cgsl剩余
     */
    private int dealWithZyData(BuyRecordBO zy, List<TempKcjlDaily> inventoryList,
                               List<TradeRecord> tradeRecordList, List<TempZyjl> tempZyjlList,
                               long kcsl, int cgsl, boolean flag, String zcOrigin, String zlOrigin,
                               String origin) {
        String zcBelongs = "";
        String zlBelongs = "";
        //数量确认
        int numRes = cgsl - (int) kcsl;
        int num = numRes < 0 ? cgsl : (int) kcsl;
        //货权确认
        if (flag) {
            zcBelongs = "FDS-" + zcOrigin;
            zlBelongs = "FDS-" + zlOrigin;
        }
        if (!flag) {
            zcBelongs = "COSTWAY-" + origin;
            zlBelongs = "COSTWAY-" + zlOrigin;
            zcOrigin = origin;
        }
        if (StringUtils.isBlank(zcBelongs) || StringUtils.isBlank(zlBelongs)) {
            log.error("zcBelongs: " + zcBelongs + "zlBelongs: " + zlBelongs);
        }
        String finalZcBelongs = zcBelongs;
        String finalZlBelongs = zlBelongs;
        //减转出库存
        inventoryList.stream().filter(l -> l.getCpbh().equals(zy.getCpbh()) &&
                        l.getPlace().equals(zy.getZcck()) && l.getBelongs().equals(finalZcBelongs))
                .forEach(l -> l.setSl(l.getSl() - num));
        if (inventoryList.stream().anyMatch(l -> l.getCpbh().equals(zy.getCpbh()) &&
                l.getPlace().equals(zy.getZlck()) && l.getBelongs().equals(finalZlBelongs))) {
            //加转入库存
            inventoryList.stream().filter(l -> l.getCpbh().equals(zy.getCpbh()) &&
                            l.getPlace().equals(zy.getZlck()) && l.getBelongs().equals(finalZlBelongs))
                    .forEach(l -> l.setSl(l.getSl() + num));
        } else {
            //新增库存
            inventoryList.add(entityManager.genTempKcjlDaily(zy.getCpbh(), zlBelongs, zy.getZlck(),
                    num, zy.getHtrq()));
        }
        //新增转运记录
        tempZyjlList.add(entityManager.genTempZyjl(zy, num, zcBelongs, zlBelongs, zcOrigin, zlOrigin));
        if (!Objects.equals(zcOrigin, zlOrigin)) {
            //新增买卖记录(company小写)
            tradeRecordList.add(entityManager.genTradeRecord(zy, num, zcBelongs.toLowerCase(),
                    zlBelongs.toLowerCase(), zcOrigin, zlOrigin, "转运"));
        }
        return Math.max(numRes, 0);
    }


    private int dealFbaOtherCK(int cgsl, BuyRecordBO zy, List<TempKcjlDaily> inventoryList,
                               List<TradeRecord> tradeRecordList, List<TempZyjl> tempZyjlList,
                               Map<String, String> metaWarehouseMap, Map<String, String> fbaCkMap,
                               Map<String, List<String>> fbaCkFdsMap,
                               Map<String, Map<String, List<String>>> fbaCkCostwayMap) {
        String zcOrigin = metaWarehouseMap.get(zy.getZcck());
        String zlOrigin = metaWarehouseMap.get(zy.getZlck());
        String fdsCk = fbaCkMap.get(zy.getZcck());
        if (StringUtils.isNotBlank(fdsCk)) {
            //FDS
            List<String> fbaFdsCkList = fbaCkFdsMap.get(fdsCk).stream()
                    .filter(l -> !l.equals(zy.getZcck())).collect(Collectors.toList());
            for (String otherCk : fbaFdsCkList) {
                String ckOrigin = metaWarehouseMap.get(otherCk);
                Long num = Optional.of(inventoryList.stream().filter(l ->
                        l.getCpbh().equals(zy.getCpbh()) && l.getPlace().equals(otherCk) &&
                                l.getBelongs().equals("FDS-" + ckOrigin)).count()).orElse(0L);
                if (num > 0 && cgsl > 0) {
                    int sum = inventoryList.stream().filter(l ->
                                    l.getCpbh().equals(zy.getCpbh()) && l.getPlace().equals(otherCk) &&
                                            l.getBelongs().equals("FDS-" + ckOrigin))
                            .mapToInt(TempKcjlDaily::getSl).sum();
                    cgsl = dealWithZyData4Fds(zy, inventoryList, tradeRecordList, tempZyjlList,
                            sum, cgsl, zcOrigin, zlOrigin, ckOrigin, otherCk);
                }
            }
            //COSTWAY
            if (cgsl > 0) {
                for (String origin : CountryEnum.getCountryList()) {
                    List<String> fbaCostwayCkList = fbaCkCostwayMap.get(fdsCk).get(origin);
                    if (!CollectionUtils.isEmpty(fbaCostwayCkList)) {
                        fbaCostwayCkList = fbaCostwayCkList.stream().filter(l -> !l.equals(zy.getZcck()))
                                .collect(Collectors.toList());
                        for (String otherCk : fbaCostwayCkList) {
                            String ckOrigin = metaWarehouseMap.get(otherCk);
                            Long num = Optional.of(inventoryList.stream().filter(l ->
                                    l.getCpbh().equals(zy.getCpbh()) && l.getPlace().equals(otherCk) &&
                                            l.getBelongs().equals("COSTWAY-" + ckOrigin)).count()).orElse(0L);
                            if (num > 0 && cgsl > 0) {
                                int sum = inventoryList.stream().filter(l ->
                                                l.getCpbh().equals(zy.getCpbh()) && l.getPlace().equals(otherCk) &&
                                                        l.getBelongs().equals("COSTWAY-" + ckOrigin))
                                        .mapToInt(TempKcjlDaily::getSl).sum();
                                cgsl = dealWithZyData4CostWay(zy, inventoryList, tradeRecordList, tempZyjlList,
                                        sum, cgsl, zcOrigin, zlOrigin, ckOrigin, otherCk, origin);
                            }
                        }
                    }
                }
            }
        }
        return cgsl;
    }


    /**
     * 处理fba的进仓数据，从其他仓库（非历史和历史）进行转运至本地仓库（数据处理）
     * 因为是从其他仓库转入仓库，具体转运记录有2条。otherCk -> zcCk -> zlCk，买卖记录亦是如此
     *
     * @param zy              转运数据
     * @param inventoryList   仓库数据
     * @param tradeRecordList 买卖记录
     * @param tempZyjlList    转运记录
     * @param kcsl            仓库数量（一定是大于0）
     * @param cgsl            采购数量（一定是大于0）
     * @param zcOrigin        转出仓库来源
     * @param zlOrigin        转入仓库来源
     * @param ckOrigin        其他仓库来源
     * @param otherCk         其他仓库
     * @return cgsl剩余
     */
    private int dealWithZyData4Fds(BuyRecordBO zy, List<TempKcjlDaily> inventoryList,
                                   List<TradeRecord> tradeRecordList, List<TempZyjl> tempZyjlList,
                                   long kcsl, int cgsl, String zcOrigin, String zlOrigin,
                                   String ckOrigin, String otherCk) {
        //货权信息
        String zcBelongs = "FDS-" + zcOrigin;
        String zlBelongs = "FDS-" + zlOrigin;
        String ckBelongs = "FDS-" + ckOrigin;
        //数量确认
        int numRes = cgsl - (int) kcsl;
        int num = numRes < 0 ? cgsl : (int) kcsl;
        if (StringUtils.isBlank(zcBelongs) || StringUtils.isBlank(zlBelongs) ||
                StringUtils.isBlank(ckBelongs)) {
            log.error("zcBelongs: " + zcBelongs + "zlBelongs: " + zlBelongs + "ckBelongs: " + ckBelongs);
        }
        //减转出库存
        inventoryList.stream().filter(l -> l.getCpbh().equals(zy.getCpbh()) &&
                        l.getPlace().equals(otherCk) && l.getBelongs().equals(ckBelongs))
                .forEach(l -> l.setSl(l.getSl() - num));
        if (inventoryList.stream().anyMatch(l -> l.getCpbh().equals(zy.getCpbh()) &&
                l.getPlace().equals(zy.getZlck()) && l.getBelongs().equals(zlBelongs))) {
            //加转入库存
            inventoryList.stream().filter(l -> l.getCpbh().equals(zy.getCpbh()) &&
                            l.getPlace().equals(zy.getZlck()) && l.getBelongs().equals(zlBelongs))
                    .forEach(l -> l.setSl(l.getSl() + num));
        } else {
            //新增库存
            TempKcjlDaily kcjlDaily = TempKcjlDaily.builder().cpbh(zy.getCpbh()).place(zy.getZlck())
                    .belongs(zlBelongs).sl(num).build();
            inventoryList.add(kcjlDaily);
        }
        //新增转运记录
        TempZyjl otherToZc = TempZyjl.builder().cpbh(zy.getCpbh()).zcck(otherCk).zlck(zy.getZcck())
                .cgsl(num).hthm(zy.getHthm()).htrq(zy.getHtrq()).frombelongs(ckBelongs)
                .tobelongs(zcBelongs).fromcountry(ckOrigin).tocountry(zcOrigin)
                .sheetnumber(zy.getSheetNumber()).build();
        tempZyjlList.add(otherToZc);
        TempZyjl zcToZl = TempZyjl.builder().cpbh(zy.getCpbh()).zcck(zy.getZcck()).zlck(zy.getZlck())
                .cgsl(num).hthm(zy.getHthm()).htrq(zy.getHtrq()).frombelongs(zcBelongs)
                .tobelongs(zlBelongs).fromcountry(zcOrigin).tocountry(zlOrigin)
                .sheetnumber(zy.getSheetNumber()).build();
        tempZyjlList.add(zcToZl);
        if (!Objects.equals(ckOrigin, zcOrigin)) {
            //新增买卖记录(company小写)
            TradeRecord tradeRecord = TradeRecord.builder().cpbh(zy.getCpbh()).zcck(otherCk)
                    .zlck(zy.getZcck()).mmsl(num).fromcompany(ckBelongs.toLowerCase())
                    .tocompany(zcBelongs.toLowerCase()).fromcountry(zcOrigin).tocountry(zlOrigin)
                    .type("转运").zyhthm(zy.getHthm()).date(zy.getHtrq()).build();
            tradeRecordList.add(tradeRecord);
        }
        if (!Objects.equals(zcOrigin, zlOrigin)) {
            //新增买卖记录(company小写)
            TradeRecord tradeRecord = TradeRecord.builder().cpbh(zy.getCpbh()).zcck(zy.getZcck())
                    .zlck(zy.getZlck()).mmsl(num).fromcompany(zcBelongs.toLowerCase())
                    .tocompany(zlBelongs.toLowerCase()).fromcountry(zcOrigin).tocountry(zlOrigin)
                    .type("转运").zyhthm(zy.getHthm()).date(zy.getHtrq()).build();
            tradeRecordList.add(tradeRecord);
        }
        return Math.max(numRes, 0);
    }

    /**
     * 处理costway的进仓数据，从其他仓库（非历史和历史）进行转运至本地仓库（数据处理）
     * 因为是从其他仓库转入，具体转运记录有2条。otherCk -> zcCk -> zlCk，买卖记录亦是如此
     *
     * @param zy              转运数据
     * @param inventoryList   仓库数据
     * @param tradeRecordList 买卖记录
     * @param tempZyjlList    转运记录
     * @param kcsl            仓库数量（一定是大于0）
     * @param cgsl            采购数量（一定是大于0）
     * @param zcOrigin        转出仓库来源
     * @param zlOrigin        转入仓库来源
     * @param ckOrigin        其他仓库来源
     * @param otherCk         其他仓库
     * @return cgsl剩余
     */
    private int dealWithZyData4CostWay(BuyRecordBO zy, List<TempKcjlDaily> inventoryList,
                                       List<TradeRecord> tradeRecordList, List<TempZyjl> tempZyjlList,
                                       long kcsl, int cgsl, String zcOrigin, String zlOrigin,
                                       String ckOrigin, String otherCk, String origin) {
        //货权信息
        String zcBelongs = "COSTWAY-" + origin;
        String zlBelongs = zcBelongs;
        //数量确认
        int numRes = cgsl - (int) kcsl;
        int num = numRes < 0 ? cgsl : (int) kcsl;
        if (StringUtils.isBlank(zcBelongs) || StringUtils.isBlank(zlBelongs)) {
            log.error("zcBelongs: " + zcBelongs + "zlBelongs: " + zlBelongs);
        }
        //----------------------- 其他仓库 -> 转出 -----------------------
        //减转出库存
        String finalZcBelongs = zcBelongs;
        inventoryList.stream().filter(l -> l.getCpbh().equals(zy.getCpbh()) &&
                        l.getPlace().equals(otherCk) && l.getBelongs().equals(finalZcBelongs))
                .forEach(l -> l.setSl(l.getSl() - num));
        String newZcBelongs;
        if (zcOrigin.equals(origin)) {
            newZcBelongs = zcBelongs;
        } else {
            newZcBelongs = "COSTWAY-" + zcOrigin;
            //新增买卖记录(company小写)
            TradeRecord other2ZctradeRecord = TradeRecord.builder().cpbh(zy.getCpbh()).zcck(otherCk)
                    .zlck(zy.getZcck()).mmsl(num).fromcompany(zcBelongs.toLowerCase())
                    .tocompany(newZcBelongs.toLowerCase()).fromcountry(ckOrigin).tocountry(zcOrigin)
                    .type("转运").zyhthm(zy.getHthm()).date(zy.getHtrq()).build();
            tradeRecordList.add(other2ZctradeRecord);
        }
        TempZyjl otherToZc = TempZyjl.builder().cpbh(zy.getCpbh()).zcck(zy.getZcck()).zlck(zy.getZlck())
                .cgsl(num).hthm(zy.getHthm()).htrq(zy.getHtrq()).frombelongs(zcBelongs)
                .tobelongs(zlBelongs).fromcountry(ckOrigin).tocountry(zcOrigin)
                .sheetnumber(zy.getSheetNumber()).build();
        tempZyjlList.add(otherToZc);
        //----------------------- 转出 -> 转入 -----------------------
        if (zlOrigin.equals(newZcBelongs.split("-")[1])) {
            zcBelongs = newZcBelongs;
            zlBelongs = newZcBelongs;
        } else {
            zcBelongs = newZcBelongs;
            zlBelongs = "COSTWAY-" + zlOrigin;
            TradeRecord zc2ZltradeRecord = TradeRecord.builder().cpbh(zy.getCpbh()).zcck(zy.getZcck())
                    .zlck(zy.getZlck()).mmsl(num).fromcompany(zcBelongs.toLowerCase())
                    .tocompany(zlBelongs.toLowerCase()).fromcountry(zcOrigin).tocountry(zlOrigin)
                    .type("转运").zyhthm(zy.getHthm()).date(zy.getHtrq()).build();
            tradeRecordList.add(zc2ZltradeRecord);
        }
        String finalZlBelongs = zlBelongs;
        if (inventoryList.stream().anyMatch(l -> l.getCpbh().equals(zy.getCpbh()) &&
                l.getPlace().equals(zy.getZlck()) && l.getBelongs().equals(finalZlBelongs))) {
            //加转入库存
            inventoryList.stream().filter(l -> l.getCpbh().equals(zy.getCpbh()) &&
                            l.getPlace().equals(zy.getZlck()) && l.getBelongs().equals(finalZlBelongs))
                    .forEach(l -> l.setSl(l.getSl() + num));
        } else {
            //新增库存
            TempKcjlDaily kcjlDaily = TempKcjlDaily.builder().cpbh(zy.getCpbh()).place(zy.getZlck())
                    .belongs(finalZlBelongs).sl(num).build();
            inventoryList.add(kcjlDaily);
        }
        TempZyjl zcToZl = TempZyjl.builder().cpbh(zy.getCpbh()).zcck(zy.getZcck()).zlck(zy.getZlck())
                .cgsl(num).hthm(zy.getHthm()).htrq(zy.getHtrq()).frombelongs(zcBelongs)
                .tobelongs(finalZlBelongs).fromcountry(ckOrigin).tocountry(zcOrigin)
                .sheetnumber(zy.getSheetNumber()).build();
        tempZyjlList.add(zcToZl);
        return Math.max(numRes, 0);
    }

    /**
     * 处理转运删除数据，对zcck为EU生资库处理替换为EU12：EU06-EU生资库-EU12
     *
     * @param zyjlList       转运数据
     * @param jcjlList       进仓数据
     * @param deleteZyRecord 转运删除数据
     * @param pkpyList       盘亏盘盈数据
     * @param inventoryList  库存数据
     */
    public void dealWithDeleteZyData(List<BuyRecordBO> zyjlList, List<BuyRecordBO> jcjlList, List<BuyRecordBO> deleteZyRecord, List<TempPkpy> pkpyList, List<TempKcjlDaily> inventoryList) {
        //------------------ 删除没有进仓记录的转运(数据收集) ------------------
        if (!CollectionUtils.isEmpty(zyjlList)) {
            for (BuyRecordBO zy : zyjlList) {
                if (inventoryList.stream().noneMatch(l -> l.getCpbh().equals(zy.getCpbh())
                        && l.getPlace().equals(zy.getZcck())) &&
                        jcjlList.stream().noneMatch(l -> l.getCpbh().equals(zy.getCpbh()) &&
                                l.getPlace().equals(zy.getZcck())) &&
                        zyjlList.stream().noneMatch(l -> l.getCpbh().equals(zy.getCpbh()) &&
                                l.getZlck().equals(zy.getZcck())) && zy.getCgsl() != 0) {
                    deleteZyRecord.add(zy);
                }
            }
        }
        //zy移除删除的记录
        if (!CollectionUtils.isEmpty(deleteZyRecord)) {
            zyjlList.removeAll(deleteZyRecord);
        }
        //判断转运删除中的zcck是否为EU生资库的,加入到盘盈列表
        deleteZyRecord.removeIf(zy -> {
            if (!"EU生资库".equals(zy.getZcck())) {
                return false;//保留元素
            }
            TempPkpy tempPkpy = TempPkpy.builder()
                    .cpbh(zy.getCpbh())
                    .place("EU12")
                    .sl(-zy.getCgsl())  // 取负数
                    .date(zy.getHtrq())
                    .hthm(zy.getHthm())
                    .build();
            String belongs;
            //对NP10437特殊处理
            if ("NP10437".equals(zy.getCpbh())) {
                belongs = "FDS-IT";
            } else {
                belongs = StringUtils.defaultIfBlank(
                        tempRelationService.getBelongsByCpbhSl(zy.getCpbh(), zy.getCgsl(), "HY-SCBYK25038730"),
                        "COSTWAY-IT");
            }
            tempPkpy.setBelongs(belongs);
            pkpyList.add(tempPkpy);
            //对盘盈数据进行数据处理修改添加到库存记录中，
            String finalBelongs = belongs;
            List<TempKcjlDaily> kcjlDailyList = inventoryList.stream()
                    .filter(l -> l.getCpbh().equals(zy.getCpbh())
                            && l.getPlace().equals(zy.getZlck())
                            && l.getBelongs().equals(finalBelongs))
                    .collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(kcjlDailyList)) {
                kcjlDailyList.forEach(l -> l.setSl(l.getSl() + zy.getCgsl()));
            } else {
                TempKcjlDaily kcjlDaily = TempKcjlDaily.builder().belongs(finalBelongs).place(zy.getZlck())
                        .cpbh(zy.getCpbh()).sl(zy.getCgsl()).build();
                inventoryList.add(kcjlDaily);
            }
            return true; // 移除该元素
        });
    }
}
